package com.example.lessontable;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;


public class TimeTableFragment extends Fragment {

    private GridView mGridview;

    /**
     * 模拟要显示的数据
     */
    String[] data = {
            "", "", "", "", "", "", "",
            "语文", "", "", "", "", "", "",
            "语文", "", "英语", "", "", "", "",
            "", "数学", "英语", "", "", "", "",
            "", "体育", "", "", "", "", "",
            "", "", "", "", "化学", "", "",
            "", "", "", "", "", "", "",
            "", "", "", "", "", "", "",
            "", "", "", "", "", "", ""};


    //gridview的每行高
    private int mH;

    //加载的布局
    private View mView;

    //今天是星期几
    private int mWeekDay;

    //系统日期
    private String mCurDate;

    String a = "2017-3-20";
    private TextView mTextdate;
    private TextView mWeek1;
    private TextView mLesson6;
    private TextView mLesson7;
    private TextView mLesson8;
    private TextView mLesson9;
    private TextView mLesson10;
    private int mMonth;
    private int mWeek;
    private LinearLayout mTimetableBG;
    private LinearLayout mTimetable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        mView = View.inflate(getActivity(), R.layout.timetable_view, null);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //初始化控件
        initView(mView);

        //设置数据
        setText();

        //获取并设置当天的日期
        mCurDate = getCurDate();
        mTextdate.setText(mCurDate);

        //获取当天是星期几并给对应的星期设置颜色
        setWeekDayColor(mCurDate);

        //设置宽度
        return mView;
    }

    /**
     * 设置数据
     */
    private void setText() {
        int a = data.length / 7;
        setLesson(a);

        //设置当天日期显示
        String curDate = getCurDate();
        mTextdate.setText(curDate);

        //设置当天是本月的第几周
        String mm = mMonth + "月第" + mWeek + "周";
        mWeek1.setText(mm);
        mWeek1.setTextSize(15);

        //给当天是星期几设置颜色
        setWeekDayColor(curDate);

        //设置适配器
        mGridview.setAdapter(new GridViewAdapter());
    }

    private void setLesson(int a) {
        switch (a) {
            case 6:
                mLesson6.setVisibility(View.VISIBLE);
                break;
            case 7:
                mLesson6.setVisibility(View.VISIBLE);
                mLesson7.setVisibility(View.VISIBLE);
                break;
            case 8:
                mLesson6.setVisibility(View.VISIBLE);
                mLesson7.setVisibility(View.VISIBLE);
                mLesson8.setVisibility(View.VISIBLE);
                break;
            case 9:
                mLesson6.setVisibility(View.VISIBLE);
                mLesson7.setVisibility(View.VISIBLE);
                mLesson8.setVisibility(View.VISIBLE);
                mLesson9.setVisibility(View.VISIBLE);
                break;
            case 10:
                mLesson6.setVisibility(View.VISIBLE);
                mLesson7.setVisibility(View.VISIBLE);
                mLesson8.setVisibility(View.VISIBLE);
                mLesson9.setVisibility(View.VISIBLE);
                mLesson10.setVisibility(View.VISIBLE);
                break;
        }
    }

    /**
     * 获取当天是星期几并设置星期几的字体颜色
     */
    private void setWeekDayColor(String date) {
        /**注意,一周是从星期日开始的*/
        //星期控件ID数组
        int[] weekday = {2, R.id.week_7, R.id.week_1, R.id.week_2, R.id.week_3,
                R.id.week_4, R.id.week_5, R.id.week_6};

        //获取今天星期几
        mWeekDay = getWeekDay(date);

        //根据星期几从控件数组中取出对应的控件ID,并找到这个控件
        TextView textView = (TextView) mView.findViewById(weekday[mWeekDay]);

        //给这个控件的文字设置颜色
        textView.setTextColor(Color.RED);
    }

    /**
     * 获取今天是星期几
     */
    private int getWeekDay(String date) {
        //格式化日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar instance = Calendar.getInstance();
        try {
            instance.setTime(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //获取星期几
        int week = instance.get(Calendar.DAY_OF_WEEK);
        return week;
    }

    /**
     * 获取当天日期
     */
    public String getCurDate() {
        //指定东8时区
        Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
        int year = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DATE);
        //本月的第几周
        mWeek = calendar.get(Calendar.WEEK_OF_MONTH);

        String time = year + "-" + mMonth + "-" + day;
        return time;
    }

    /**
     * 适配器
     */
    private class GridViewAdapter extends BaseAdapter {


        private ViewHolder mViewHolder;

        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public Object getItem(int position) {
            return data[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            getHeight();
            if (convertView == null) {
                convertView = View.inflate(getActivity(), R.layout.timetable_item_view, null);
                mViewHolder = new ViewHolder();
                mViewHolder.mBtitem = (Button) convertView.findViewById(R.id.tv_class_item);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }

            mViewHolder.mBtitem.setText(data[position]);
            mViewHolder.mBtitem.setHeight(mH);
            /*if (!data[position].equals("")) {                        //有课
                mViewHolder.mBtitem.setBackground(getResources().getDrawable(R.drawable.button_class_selector));
            } else {                                                 //没课
                mViewHolder.mBtitem.setBackground(getResources().getDrawable(R.drawable.button_class2_selector));
            }*/

            mViewHolder.mBtitem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean Focus) {
                    if (Focus) {
                        //获取到焦点
                        getFocus();
                    } else {
                        //失去焦点
                        loseFocus();
                    }
                }
            });
            return convertView;
        }
    }

    /**
     * viewholder
     */
    protected class ViewHolder {
        //声明需要用到的控件
        Button mBtitem;
    }

    /**
     * 初始化控件
     */
    private void initView(View view) {
        //课程显示控件
        mGridview = (GridView) view.findViewById(R.id.gridview);

        //右上角,当天日期显示
        mTextdate = (TextView) view.findViewById(R.id.date);

        //最上方居中,本周是第几周的显示
        mWeek1 = (TextView) view.findViewById(R.id.week_of_month);

        //显示一天有几节课
        mLesson6 = (TextView) view.findViewById(R.id.timetable_lesson_6);
        mLesson7 = (TextView) view.findViewById(R.id.timetable_lesson_7);
        mLesson8 = (TextView) view.findViewById(R.id.timetable_lesson_8);
        mLesson9 = (TextView) view.findViewById(R.id.timetable_lesson_9);
        mLesson10 = (TextView) view.findViewById(R.id.timetable_lesson_10);

    }

    /**
     * 获取gridview每行的高度
     */
    public void getHeight() {
        ViewTreeObserver vto = mGridview.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mGridview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //gridview的高度
                int height = mGridview.getHeight();
                //获取行数
                int rowls = (int) Math.ceil(data.length / mGridview.getNumColumns());
                //每行高
                mH = height / rowls - 1;
            }
        });
    }

    /**获取到焦点动画*/
    public void getFocus(){
        ObjectAnimator objectAnimator_1 = ObjectAnimator.ofFloat(this,"scaleX",1f,1.3f);
        ObjectAnimator objectAnimator_2 = ObjectAnimator.ofFloat(this,"scaleY",1f,1.3f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(300);
        animatorSet.playTogether(objectAnimator_1,objectAnimator_2);
        animatorSet.start();
    }

    /**失去焦点动画*/
    public void loseFocus(){
        ObjectAnimator objectAnimator_1=ObjectAnimator.ofFloat(this,"scaleX",1.3f,1f);
        ObjectAnimator objectAnimator_2=ObjectAnimator.ofFloat(this,"scaleY",1.3f,1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(3000);
        animatorSet.playTogether(objectAnimator_1,objectAnimator_2);
        animatorSet.start();

    }

}


